home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / RenderText.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  3.2 KB  |  109 lines

  1. #include <extras/math.h>
  2. #include <proto/graphics.h>
  3. #include <proto/utility.h>
  4. #include <extras/ext_text.h>
  5. #include <clib/extras_protos.h>
  6. #include <string.h>
  7.  
  8. /****** extras.lib/gui_RenderText ******************************************
  9. *
  10. *   NAME
  11. *       gui_RenderTextA -- Write text into a RastPort.
  12. *       gui_RenderText -- varargs stub.
  13. *
  14. *   SYNOPSIS
  15. *       reserved = gui_RenderTextA( RP, String, TagList)
  16. *
  17. *       LONG gui_RenderTextA( struct RastPort *, STRPTR, 
  18. *                         struct TagItem *);
  19. *
  20. *       reserved = gui_RenderText( RP, String, Tags, ... )
  21. *
  22. *       LONG gui_RenderText( struct RastPort *, STRPTR, 
  23. *                        Tag, ...);
  24. *
  25. *   FUNCTION
  26. *       Writes text into a Rastport, basically an interface
  27. *       to the graphics.library/Text() function.
  28. *
  29. *   INPUTS
  30. *       RP - rastport to write to.
  31. *       String - the string to write.
  32. *       TagList - an array of TagItems.
  33. *         RT_Baseline - baseline of the cursor. 
  34. *                       default RastPort->cp_y
  35. *         RT_XPos     - horizontal position of the cursor.
  36. *                       default RastPort->cp_x
  37. *         RT_MaxWidth - maximum pixel length of text, excess
  38. *                       characters will be clipped.
  39. *         RT_Justification - RTJ_???  (default _LEFT)
  40. *         RT_TextFont - struct TextFont * from OpenFont()
  41. *         RT_Strlen   - number of characters in string.
  42. *         RT_TextLength - (ULONG *) Width in pixels of printed texted.
  43. *
  44. *
  45. *   RESULT
  46. *       Number of characters drawn.
  47. *
  48. *   BUGS
  49. *       RenderText does not reset a RastPort's TextFont
  50. *       after using RT_TextFont. 
  51. *
  52. *   SEE ALSO
  53. *       mlr_rendertext.image image class.
  54. *
  55. ******************************************************************************
  56. *
  57. */
  58.  
  59.  
  60. LONG gui_RenderText(struct RastPort *RP, STRPTR String, Tag Tags, ... )
  61. {
  62.   return(gui_RenderTextA(RP,String,(struct TagItem *)&Tags));
  63. }
  64.  
  65. LONG gui_RenderTextA(struct RastPort *RP, STRPTR String, struct TagItem *TagList)
  66. {
  67.   struct TextFont *rt_textfont;
  68.   LONG    rt_xpos, rt_baseline, rt_maxwidth, rt_justification,
  69.           rt_strlen=0, *rt_textlength;
  70.   struct TextExtent extent;
  71.   LONG   len;
  72.   
  73.   if(RP && String)
  74.   {
  75.     rt_xpos         =GetTagData(RT_XPos    , RP->cp_x     , TagList);
  76.     rt_baseline     =GetTagData(RT_Baseline, RP->cp_y     , TagList);
  77.     rt_maxwidth     =GetTagData(RT_MaxWidth, -1           , TagList);
  78.     rt_justification=GetTagData(RT_Justification, RTJ_LEFT, TagList);
  79.     rt_strlen       =GetTagData(RT_Strlen, strlen(String) , TagList);
  80.     if(rt_textfont =(struct TextFont *)GetTagData(RT_TextFont, 0, TagList))
  81.       SetFont(RP,rt_textfont);
  82.  
  83.     if(rt_maxwidth>=0)
  84.     {
  85.       rt_strlen=TextFit(RP,String,rt_strlen,&extent,NULL,1,
  86.                       rt_maxwidth,32767);
  87.     }
  88.     len=TextLength(RP,String,rt_strlen);
  89.     if(rt_textlength=(ULONG *)GetTagData(RT_TextLength, 0 , TagList))
  90.     {
  91.       *rt_textlength=len;
  92.     }
  93.     switch(rt_justification)
  94.     {
  95.       //case RTJ_LEFT:
  96.       //  break;
  97.       case RTJ_CENTER:
  98.         rt_xpos=rt_xpos-(len/2);
  99.         break;
  100.       case RTJ_RIGHT:
  101.         rt_xpos-=len;
  102.         break;
  103.     }
  104.     Move(RP,rt_xpos,rt_baseline);
  105.     Text(RP,String,rt_strlen);
  106.   }
  107.   return(rt_strlen);
  108. }
  109.